package springboot.service.impl;

import org.springframework.stereotype.Service;
import springboot.Tools.time;
import springboot.dao.TbProblemStatusDao;
import springboot.doMain.Msg;
import springboot.doMain.OverTimeData;
import springboot.doMain.TbProblemStatus;
import springboot.service.tbProblemStatusService;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TbProblemStatusServiceImpl implements tbProblemStatusService {
	
	final
	TbProblemStatusDao tbProblemStatusDao;
	
	public TbProblemStatusServiceImpl(TbProblemStatusDao tbProblemStatusDao) {
		this.tbProblemStatusDao = tbProblemStatusDao;
	}
	
	@Override
	public TbProblemStatus lastStatus(int idProblemStatus) {
		return tbProblemStatusDao.lastStatus(idProblemStatus);
	}
	
	@Override
	public int insertProblemStatusContent(TbProblemStatus tbProblemStatus) {
		
		try {
			return tbProblemStatusDao.insertProblemStatusContent(tbProblemStatus);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	@Override
	public List<TbProblemStatusDao> selAll() {
		return null;
	}
	
	@Override
	public List<Map<String, Object>> selEngineerNeedAddProblemStatusCount() {
		
		for (Msg msg : tbProblemStatusDao.problemNoStatus()) {
			if (tbProblemStatusDao.selProblemCount(msg.getHq()) == 0) {
				msg.setDate(new Date((msg.getTime())));
				msg.setMsgStatus("问题生成");
				try {
					tbProblemStatusDao.addProblemStatus(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return tbProblemStatusDao.selEngineerNeedAddProblemStatusCount();
	}
	
	@Override
	public List<Map<String, Object>> selEngineerNeedToReplyNum() {
		List<Map<String, Object>> engineerNeedToReplyNumList;
		try {
			engineerNeedToReplyNumList = tbProblemStatusDao.selEngineerNeedToReplyNum((System.currentTimeMillis() ) - 3600000 * 14);
			return engineerNeedToReplyNumList;
		} catch (Exception e) {
			System.err.println("selEngineerNeedToReplyNum = " + e);
			return null;
		}
		
	}
	
	
	@Override
	public List<TbProblemStatus> problemStatusByHq(int hq) {
		List<TbProblemStatus> tbProblemStatusesList = null;
		try {
			tbProblemStatusesList = tbProblemStatusDao.problemStatusByHq(hq);
			for (TbProblemStatus tbProblemStatus : tbProblemStatusesList) {
				tbProblemStatus.setTimeFormat(time.dateFormat(tbProblemStatus.getTime()));
			}
			return tbProblemStatusesList;
		} catch (Exception e) {
			System.err.println("problemStatusByHq = " + e);
		}
		return tbProblemStatusesList;
	}
	
	@Override
	public List<OverTimeData> selOverTimeData() {
		return tbProblemStatusDao.selOverTimeData();
	}
	
	//    需要为问题添加状态
	@Override
	public List<Map<String, Object>> problemNeedAddStatus() {
		
		for (Msg msg : tbProblemStatusDao.problemNoStatus()) {
			if (tbProblemStatusDao.selProblemCount(msg.getHq()) == 0) {
				msg.setDate(new Date(msg.getTime()));
				tbProblemStatusDao.addProblemStatus(msg);
			}
		}
		return null;
	}
	
	@Override
	public List<Msg> problemNoStatus() {
		
		return null;
	}
	
	@Override
	public int del_pro_status(int idProblemStatus) {
		tbProblemStatusDao.del_pro_status(idProblemStatus);
		return 0;
	}
	
	
}
